﻿#pragma once
#include "afxdialogex.h"
#include <iostream>
#include <sstream>
#include <string>
#include <json.h>
#include "StringUtil.h"
#include <mysql.h>
#include <vector>
#include <cmath>
#include <ctime>
#include <iomanip> // ??????????????

#ifdef _DEBUG
#pragma comment(lib,"libmysql.lib")
#else
#pragma comment(lib,"libmysql.lib")
#endif

#define BUF_SQL_LEN 4096

struct SensorInfo {
	int id;
	std::string Num;
	std::string bus;
	std::string port;
	std::string show_value;
	std::string times;
	std::string zero_position;
	std::string warning_limit;
};

struct AlarmEventRecord {
	int id;
	std::string alarm_type;
	std::string handle_content;
	std::string time;
	std::string operator_name;
	std::string product_type;
	std::string sg_num;
};

struct AlarmTip {
	int id;
	std::string alarm_content;
	std::string time;
	std::string operator_name;
	std::string product_type;
};

struct DataRecord {
	int id;
	int ms_slot_qf;
	std::string sn_no;
	std::string product_type;
	std::string p1_th_val;
	std::string p1_ms_val;
	std::string p1_qf;
	std::string p2_th_val;
	std::string p2_ms_val;
	std::string p2_qf;
	std::string p3_th_val;
	std::string p3_ms_val;
	std::string p3_qf;
	std::string ct_val;
	std::string s1_bt_val;
	std::string s1_bc_val;
	std::string s2_bt_val;
	std::string s2_bc_val;
	std::string s3_bt_val;
	std::string s3_bc_val;
	std::string s4_bt_val;
	std::string s4_bc_val;
	std::string batch_num;
	std::string card_num;
	std::string card_quantity;
	std::string team_num;
	std::string operator_name;
	std::string time;
	std::string data_legal;
	std::string sg_num_sign;
	std::string sg_num;
	std::string cn_sg_num;
	std::string cn_teams_num;
	std::string product_num;
	std::string procedure_num;
	std::string device_id;
	std::string ct_vf_time;
	std::string data_1;
	std::string data_2;
	std::string data_3;
	std::string data_4;
	std::string data_5;
	std::string data_6;
	std::string data_7;
	std::string data_8;
	std::string data_9;
	std::string data_10;
	std::string ct_bc_val;
	
	std::string define_44;
	std::string define_45;
	std::string define_46;
	std::string define_47;
	std::string define_48;
	std::string status;

	std::string data_rod;
	std::string rod_tips;
	std::string abnor_sign;
	double rods[15];


	double rod1_mean;
	double rod2_mean;
	double rod3_mean;

	double slot1_mean;
	double slot2_mean;
	double slot3_mean;
	double slot4_mean;
	double slot5_mean;
	double slot6_mean;
	double slot7_mean;
	double slot8_mean;
	double slot9_mean;


	int point1, point2, point3, point4, point5, point6, point7, point8, point9;
	std::vector<int> rod1_point;
	std::vector<int> rod2_point;
	std::vector<int> rod3_point;

	double rod1_range;
	double rod2_range;
	double rod3_range;

	std::vector<double> p1_ms_val_data;
	std::vector<double> p2_ms_val_data;
	std::vector<double> p3_ms_val_data;

	std::vector<double> rod1_mean_data;
	std::vector<double> rod2_mean_data;
	std::vector<double> rod3_mean_data;

	std::vector<double> rod1_range_data;
	std::vector<double> rod2_range_data;
	std::vector<double> rod3_range_data;

	std::vector<double> ct_ms_val_data;
	std::vector<double> s1_ms_val_data;
	std::vector<double> s2_ms_val_data;
	std::vector<double> s3_ms_val_data;
	std::vector<double> s4_ms_val_data;
	std::vector<double> s1_bc_val_data;
	std::vector<double> s2_bc_val_data;
	std::vector<double> s3_bc_val_data;
	std::vector<double> s4_bc_val_data;

	std::vector<double> slot1_mean_data;
	std::vector<double> slot2_mean_data;
	std::vector<double> slot3_mean_data;
	std::vector<double> slot4_mean_data;
	std::vector<double> slot5_mean_data;
	std::vector<double> slot6_mean_data;
	std::vector<double> slot7_mean_data;
	std::vector<double> slot8_mean_data;
	std::vector<double> slot9_mean_data;

	std::vector<double> slot1_range_data;
	std::vector<double> slot2_range_data;
	std::vector<double> slot3_range_data;
	std::vector<double> slot4_range_data;
	std::vector<double> slot5_range_data;
	std::vector<double> slot6_range_data;
	std::vector<double> slot7_range_data;
	std::vector<double> slot8_range_data;
	std::vector<double> slot9_range_data;

	std::vector<int> slot1_point;
	std::vector<int> slot2_point;
	std::vector<int> slot3_point;
	std::vector<int> slot4_point;
	std::vector<int> slot5_point;
	std::vector<int> slot6_point;
	std::vector<int> slot7_point;
	std::vector<int> slot8_point;
	std::vector<int> slot9_point;
};


struct HoselLinearityRecord {
	int id;
	std::string sn_no;
	std::string product_type;
	std::string line_th_val;
	std::string line_ms_val;
	std::string line_qf;
	std::string round_th_val;
	std::string round_ms_val;
	std::string round_qf;
	std::string batch_num;
	std::string card_num;
	std::string card_quantity;
	std::string team_num;
	std::string _operator; //operator??C++?????????????????????????????????
	std::string time;
	std::string product_num;
	std::string procedure_num;
	std::string device_id;

};

struct HoselRoundRecord {
	int id;
	std::string sn_no;
	std::string product_type;
	std::string p1_th_val;
	std::string p1_ms_val;
	std::string p1_qf;
	std::string batch_num;
	std::string card_num;
	std::string card_quantity;
	std::string team_num;
	std::string _operator; //operator??C++?????????????????????????????????
	std::string time;
	std::string product_num;
	std::string procedure_num;
	std::string device_id;
};

//?????
struct LoginUser {
	int id;
	std::string name;
	std::string passwd;//?????????
	std::string team;
	std::string leader_name;
	int leader;
	int role;
	int status;
	int code;
};

struct ProductParamInfo {
	int id;
	std::string product_type;
	std::string p1_nml_val;
	std::string p1_nml_up;
	std::string p1_nml_down;
	std::string p1_std_val;
	std::string p1_std_up;
	std::string p1_std_down;
	std::string p1_safety;
	std::string p1_offset;
	std::string p1_error;
	std::string p1_m_up;
	std::string p1_m_ct;
	std::string p1_m_down;
	std::string p1_r_up;
	std::string p1_r_ct;
	std::string p1_r_down;
	std::string p2_nml_val;
	std::string p2_nml_up;
	std::string p2_nml_down;
	std::string p2_std_val;
	std::string p2_std_up;
	std::string p2_std_down;
	std::string p2_safety;
	std::string p2_offset;
	std::string p2_error;
	std::string p2_m_up;
	std::string p2_m_ct;
	std::string p2_m_down;
	std::string p2_r_up;
	std::string p2_r_ct;
	std::string p2_r_down;
	std::string p3_nml_val;
	std::string p3_nml_up;
	std::string p3_nml_down;
	std::string p3_std_val;
	std::string p3_std_up;
	std::string p3_std_down;
	std::string p3_safety;
	std::string p3_offset;
	std::string p3_error;
	std::string p3_m_up;
	std::string p3_m_ct;
	std::string p3_m_down;
	std::string p3_r_up;
	std::string p3_r_ct;
	std::string p3_r_down;
	std::string std_pf_min;
	std::string min_btn_tms;
	std::string batch_num;
	std::string device_id;
	std::string card_num;
	std::string cn_time;
	std::string cn_teams_num;
	std::string cn_sg_num;
	std::string sh_bg_time;
	std::string sh_sg_num;
	std::string sg_num;
	std::string data_rod;
	std::string rod_tips;
	std::string s_sign;
	std::string remark;
	float data[9];
	std::string error_alarm;

	std::string nml_val;
	std::string nml_up;
	std::string nml_down;
	std::string std_val;
	std::string std_up;
	std::string std_down;
	std::string error_val;
	std::string error_up;
	std::string error_down;
	std::string safety;
	std::string rod_round;
	std::string rod_line;

	std::string rod_x_round;
	std::string rod_x_line;
	std::string rod_r_round;
	std::string rod_r_line;
};

struct ProductParam2Info {
	int id;
	std::string product_type;
	std::string p1_nml_value;
	std::string p1_nml_up;
	std::string p1_nml_down;
	std::string p1_std_val;
	std::string p1_std_up;
	std::string p1_std_down;
	std::string p1_safety;
	std::string p1_offset;
	std::string p1_error;
	std::string p1_m_up;
	std::string p1_m_ct;
	std::string p1_m_down;
	std::string p1_r_up;
	std::string p1_r_ct;
	std::string p1_r_down;
	std::string p2_nml_value;
	std::string p2_nml_upper;
	std::string p2_nml_lower;
	std::string p2_std_val;
	std::string p2_std_up;
	std::string p2_std_down;
	std::string p2_safety;
	std::string p2_offset;
	std::string p2_error;
	std::string p2_m_up;
	std::string p2_m_ct;
	std::string p2_m_down;
	std::string p2_r_up;
	std::string p2_r_ct;
	std::string p2_r_down;
	std::string sh_bg_time;
	std::string sh_sg_num;
	std::string remark;
};

struct ProofedAdSensorsValue {
	int id;
	std::string product_type;
	std::string ch1_zero_position;
	std::string ch2_zero_position;
	std::string ch3_zero_position;
};

struct UserInfo {
	int id;
	std::string name;
	std::string password;
	std::string power;
	std::string working_team;
};

struct ConicalSurfaceAngleRecord {
	int id;
	std::string sn_no;
	std::string product_type;
	std::string p1_th_val;
	std::string p1_ms_val;
	std::string p1_qf;
	std::string batch_num;
	std::string card_num;
	std::string card_quantity;
	std::string team_num;
	std::string _operator; //operator??C++?????????????????????????????????
	std::string time;
	std::string product_num;
	std::string procedure_num;
	std::string device_id;
};

struct MotorControl {
	int id;
	std::string location;
	std::string distance;
	std::string m_time;
	std::string r_speed;
	std::string m_speed;
	std::string s_sign;
	std::string remark;
};

struct TempTable {
	int id;
	std::string content;
};

struct ProductAllData {
	int id;
	std::string name;
};

struct SlotParamInfo {
	int id;
	Json::Value ct;//
	std::string product_type;//??????
	std::string sn_no;//??????
	std::string ms_slot_qf;//???§Ó????
	std::string sg_num;//???????
	std::string time;//??????
	std::string cn_teams_num;//????????
	std::string cn_sg_num;//??????????
	std::string rod_val;//????
	std::string ct_val;//?????
	std::string ct_up;//?????????
	std::string ct_down;//?????????
	std::string std_time;//???§µ???????
	std::string each_interval;//?????
	std::string fq_time;//?????????
	std::string each_time;//????????
	std::string error_alarm;//??????????
	std::string safety;
	std::string select_slot;
	std::string bt_m_up;//??????????
	std::string bt_m_ct;//????????????
	std::string bt_m_down;//??????????
	std::string bt_r_up;//???????????
	std::string bt_r_ct;//?????????????
	std::string bt_r_down;//???????????
	std::string bc_m_up;//???????????
	std::string bc_m_ct;//?????????????
	std::string bc_m_down;//???????????
	std::string bc_r_up;//????????????
	std::string bc_r_ct;//??????????????
	std::string bc_r_down;//????????????
	std::string ct_m_up;//???????????
	std::string ct_m_ct;//?????????????
	std::string ct_m_down;//???????????
	std::string ct_r_up;//???????????
	std::string ct_r_ct;//?????????????
	std::string ct_r_down;//???????????
	Json::Value slot_1;//
	Json::Value slot_2;//
	Json::Value slot_3;//
	Json::Value slot_4;//
};

struct ConeParamInfo {
	int id;
	std::string product_type;//??????
	std::string std_pf_min;
	std::string min_btn_tms;
	std::string cn_time;//??????
	std::string cn_teams_num;//????????
	std::string cn_sg_num;//??????????
	std::string sg_num;//??????
	std::string error_alarm;//??????????
	std::string rod_angle;
	std::string dial_angle;
	std::string rod_or_dial;
	Json::Value rod_g;//
	Json::Value dial_g;//
	Json::Value cone_g;//
	Json::Value cone_a;//
	Json::Value cone_r;//
};

struct SPCData {
	BSTR jsonString;
	std::vector<double> m_p1_datas; // ???
	std::vector<double> m_p2_datas;
	std::vector<double> m_p3_datas;

	std::vector<double> ct_val; //???§Ó?
	std::vector<double> s1_bt_val;
	std::vector<double> s1_bc_val;
	std::vector<double> s2_bt_val;
	std::vector<double> s2_bc_val;
	std::vector<double> s3_bt_val;
	std::vector<double> s3_bc_val;
	std::vector<double> s4_bt_val;
	std::vector<double> s4_bc_val;

	//???x???
	std::vector<double> p1_mean;
	std::vector<double> p2_mean;
	std::vector<double> p3_mean;

	//???§Ó?x???
	std::vector<double> ct_mean;
	std::vector<double> ct_bc;
	std::vector<double> s1_mean;
	std::vector<double> s2_mean;
	std::vector<double> s3_mean;
	std::vector<double> s4_mean;
	std::vector<double> s5_mean;
	std::vector<double> s6_mean;
	std::vector<double> s7_mean;
	std::vector<double> s8_mean;

	double mean1;
	double mean2;
	double mean3;
	double range1;
	double range2;
	double range3;
	std::vector<double>::iterator r_max;
	std::vector<double>::iterator r_min;
	double r_result;

	std::string product_type;
	std::string _operator;
	std::string batch_num;
	std::string team_num;
	std::string sn_no;
	std::string card_quantity;
	std::string device_id;
	std::string remark;
	std::string status; //?§Ø???????????
};

class DataBase
{
private:

public:
	MYSQL m_connect;
	MYSQL_RES* m_result;
	MYSQL_ROW m_rows;

	SensorInfo m_sensor_info;
	AlarmEventRecord m_event_record;
	AlarmTip m_alarm_tip;
	DataRecord m_data_record, m_rod_data_record, m_slot_data_record;
	HoselLinearityRecord m_hosel_linearity_record;
	ProductParamInfo m_product_param_info_all, m_product_param_info, m_product_select_info_id, m_product_all_id, m_product_paramer;
	LoginUser m_user_info, m_user_get;
	MotorControl m_motor_pm;
	SlotParamInfo m_slot_pm;
	ConeParamInfo m_cone_pm;
	Json::Value m_userInfo;
	SPCData m_spc_data;
	int m_pro_id;
	double data_rod[15];
	std::string data_rod_string;

	JSONCPP_STRING errs;
	Json::Value msgjson, root, resultJson;
	Json::CharReaderBuilder readerBuilder;

	std::string p1_qf;
	std::string p2_qf;
	std::string p3_qf;

	std::string data_legal;


	int port = 3306; //????
	char localhost[BUF_LEN] = "127.0.0.1";// ?????ip???
	//char localhost[BUF_LEN] = "192.168.31.223";// ?????ip???
	char user[BUF_LEN] = "root"; //??????????
	char password[BUF_LEN] = "123456";// ????

	char gan_jin_detector[BUF_LEN] = "gan_jin_detector"; //?????????????
	char suo_cao_detector[BUF_LEN] = "suo_cao_detector"; //?????????????
	char zui_mian_detector[BUF_LEN] = "zhui_mian_detector"; //?????????????

	std::string m_str;
	char m_un_str[BUF_LEN];
	char m_sql[BUF_SQL_LEN];

	float m_number;
	//SPC ????????
	int detect_sgin = 0;
	std::string sg_num_sign;
	int sg_num_int = 1;
	std::vector<Json::Value> rodJson;
	std::vector<Json::Value> slotJson;
	std::vector<double> m_r_data;
	std::vector<double> m_s_data;

	std::stringstream ss;
	std::vector<SensorInfo> m_sensor;
	std::vector<AlarmEventRecord> m_event;
	std::vector<AlarmTip> m_alarm;
	std::vector<DataRecord> m_data, m_rod_data, m_slot_data;
	std::vector<HoselLinearityRecord> m_hosel;
	std::vector<ProductParamInfo> m_product_all, m_product_info, m_product_select_id, m_product_set;
	std::vector<LoginUser> m_login_user, m_login_user_get;
	std::vector<MotorControl> m_motor;
	std::vector<SlotParamInfo> m_slot;
	std::vector<ConeParamInfo> m_cone;

	//=============================???SPC=============================
	int p_sign = 0;
	std::vector<SPCData> m_rod_spc;
	std::string m_rod_error;
	std::string rod1_m_up_A;//???1???????
	std::string rod1_m_ct_B;//???1?????????
	std::string rod1_m_down_C;//???1???????
	std::string rod2_m_up_A;//???1???????
	std::string rod2_m_ct_B;//???1?????????
	std::string rod2_m_down_C;//???1???????
	std::string rod3_m_up_A;//???1???????
	std::string rod3_m_ct_B;//???1?????????
	std::string rod3_m_down_C;//???1???????

	//=============================???§Ó?SPC=============================
	int s_sign = 0;
	int m_s_sign = 0;
	std::vector<SPCData> m_spc;
	std::string m_error;
	std::string s_bt_m_up;//??????????
	std::string s_bt_m_ct;//????????????
	std::string s_bt_m_down;//??????????
	std::string s_ct_m_up;//???????????
	std::string s_ct_m_ct;//?????????????
	std::string s_ct_m_down;//???????????

	std::string s1_bt_m_up;//??????????
	std::string s1_bt_m_ct;//????????????
	std::string s1_bt_m_down;//??????????
	std::string s1_ct_m_up;//???????????
	std::string s1_ct_m_ct;//?????????????
	std::string s1_ct_m_down;//???????????
	std::string s2_bt_m_up;//??????????
	std::string s2_bt_m_ct;//????????????
	std::string s2_bt_m_down;//??????????
	std::string s3_bt_m_up;//??????????
	std::string s3_bt_m_ct;//????????????
	std::string s3_bt_m_down;//??????????
	std::string s4_bt_m_up;//??????????
	std::string s4_bt_m_ct;//????????????
	std::string s4_bt_m_down;//??????????
	std::string s1_bc_m_up;
	std::string s1_bc_m_ct;
	std::string s1_bc_m_down;
	std::string s2_bc_m_up;
	std::string s2_bc_m_ct;
	std::string s2_bc_m_down;
	std::string s3_bc_m_up;
	std::string s3_bc_m_ct;
	std::string s3_bc_m_down;
	std::string s4_bc_m_up;
	std::string s4_bc_m_ct;
	std::string s4_bc_m_down;


	bool OnOpenDataBase(char* database);
	void OnCloseDataBase();
	BOOL OnInsertAlarmTips(BSTR alarm_content, int m_sys_model);
	BOOL OnInsertAlarmEventrecord(BSTR alarm_typ, BSTR handle_content, int m_sys_model);
	BSTR DataBase::OnGetProductName(int id, int m_sys_model);
	std::vector<AlarmEventRecord> OnGetAlarmEventrecord(BSTR time, int m_sys_model);
	std::vector<AlarmTip> OnGetAlarmTips(BSTR time, int m_sys_model);
	std::vector<DataRecord> OnGetDataRecord(int id, int m_sys_model);
	std::vector<DataRecord> OnGetDataRecordXR(BSTR time, int m_sys_model);
	std::vector<HoselLinearityRecord> OnGetHoselLRRecord(int id);
	BOOL OnInsertHoseRecord(BSTR valueStr);
	std::vector<ProductParamInfo> OnGetAllProduct(int m_sys_model);
	ProductParamInfo OnSelectProductInfoToSetPageByID(int id);
	ProductParamInfo GetProductAllInfoById(int id, int m_sys_model);
	ProductParamInfo OnSelectProductByID(int id);
	LoginUser OnUserLogin(BSTR user, BSTR passwd);
	LoginUser OnGetUserInfo();
	std::vector<LoginUser> OnGetUsers();
	BOOL OnDeleteUserByID(int id);
	BOOL OnInsertUser(BSTR name, BSTR passwd, BSTR team, INT leader, INT role);
	BOOL OnInsertProductParmaer(BSTR valueStr);
	BOOL OnUpdateProductParmaer(BSTR valueStr);
	BOOL OnUpdateSlotParmaer(BSTR valueStr);
	ProductParamInfo GetProductParamer(BSTR product_type);
	SlotParamInfo GetSlotParamer(BSTR product_type);
	ConeParamInfo GetConeParamer(BSTR product_type);
	BOOL DataBase::OnInsertSlotParmaer(BSTR valueStr);
	BOOL OnSendDataRecord(BSTR jsonString, int m_sys_model, int error_point1, int error_point2, int error_point3, int error_point4, int error_point5, int m_sys_mode6, int error_point7, int error_point8, int error_point9);
	BOOL OnDeleteDataRrcord(int m_sys_model);
	BOOL OnSendRodData(DataRecord rod_data);
	BOOL OnDeleteProductByID(int id, int m_sys_model);
	std::vector<MotorControl> OnGetMotorControl();
	BSTR OnGetLastProduct();


	Json::Value OnGetLockParmer();
	BOOL OnUpdataParmer(BSTR jsonString, int m_sys_model);
	std::vector<SPCData> OnErrorDataDetection(BSTR jsonString, int m_sys_model, std::string m_sg_num, bool allNormal_rob, bool allNorma2_rob, bool allNorma3_rob, bool allNormal_slot, bool allNorma2_slot, bool allNorma3_slot, bool allNorma4_slot, bool allNorma5_slot, bool allNorma6_slot, bool allNorma7_slot, bool allNorma8_slot, bool allNorma9_slot);
	std::vector<DataRecord> SelectDataRecordByTime(BSTR m_time, int m_sys_model);
	Json::Value SelectDataRecordByRodSelect(BSTR jsonString, int m_sys_model);
	Json::Value GetALLDataRecord(BSTR jsonString, int m_sys_model);
	BOOL OnUpdateConeParmaer(BSTR valueStr);
	BOOL OnInsertConeParmaer(BSTR valueStr);
};

